package com.lam.system.service.impl;

import java.util.List;

import com.lam.common.utils.StringUtils;
import com.lam.system.entity.SysUserRole;
import com.lam.system.mapper.SysUserRoleMapper;
import com.lam.system.param.SysUserRoleParam;
import com.lam.system.service.SysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**
 * 用户角色Service业务层处理
 * 
 * @author admin
 * @date 2022-09-01
 */
@Service
public class SysUserRoleServiceImpl implements SysUserRoleService {


    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    /**
     * 查询用户角色
     * 
     * @param id ID
     * @return 单个对象
     */
    @Override
    @Transactional(readOnly = true)
    public SysUserRole selectById(String id) {

        return sysUserRoleMapper.selectById(id);
    }

    /**
     * 查询用户角色列表
     * 
     * @param param
     * @return 
     */
    @Override
    @Transactional(readOnly = true)
    public List<SysUserRole> selectList(SysUserRoleParam param) {
    
        QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(param.getUserId()), "user_id", param.getUserId());
        queryWrapper.eq(StringUtils.isNotBlank(param.getRoleId()), "role_id", param.getRoleId());

        return sysUserRoleMapper.selectList(queryWrapper);
    }

    /**
     * 新增用户角色
     * 
     * @param sysUserRole 
     * @return 
     */
    @Override
    @Transactional
    public int saveByRole(SysUserRoleParam param) {
    
    	SysUserRole sysUserRole = new SysUserRole();
    	sysUserRole.setRoleId(param.getRoleId());
        
        int n = 0;
        String[] userIdArr = param.getUserId().split(",");
        for (String userId : userIdArr) {
        	sysUserRole.setUserId(userId);
        	sysUserRole.setId(null);
        	n += sysUserRoleMapper.insert(sysUserRole);
		}
        
        return n;
    }
    
    /**
     * 新增用户角色
     * 
     * @param sysUserRole 
     * @return 
     */
    @Override
    @Transactional
    public int saveByUser(SysUserRoleParam param) {
    
        SysUserRole sysUserRole = new SysUserRole();
        sysUserRole.setUserId(param.getUserId());
        
        int n = 0;
        String[] roleIdArr = param.getRoleId().split(",");
        for (String roleId : roleIdArr) {
        	sysUserRole.setRoleId(roleId);
        	sysUserRole.setId(null);
        	n += sysUserRoleMapper.insert(sysUserRole);
		}
        
        return n;
    }

    /**
     * 修改用户角色
     * 
     * @param sysUserRole 
     * @return 
     */
    @Override
    @Transactional
    public int update(SysUserRole sysUserRole) {
    
        return sysUserRoleMapper.updateById(sysUserRole);
    }

    /**
     * 删除用户角色信息
     * 
     * @param idStr ID，多个用逗号分隔
     * @return 
     */
    @Override
    @Transactional
    public int deleteByUser(String userId, String roleId) {
    	if(StringUtils.isBlank(userId)) {
            return 0;
    	}
        return sysUserRoleMapper.deleteByUserId(userId, StringUtils.str2List(roleId));
    }
    
    /**
     * 删除用户角色信息
     * 
     * @param idStr ID，多个用逗号分隔
     * @return 
     */
    @Override
    @Transactional
    public int deleteByRole(String roleId, String userId) {
    	
    	if(StringUtils.isBlank(roleId)) {
    		return 0;
    	}
    	
    	return sysUserRoleMapper.deleteByRoleId(roleId, StringUtils.str2List(userId));
    }
    
    
}
